/* $RCSFile: CountryInfoSerializer.cs $
 * Apache license v2 2011 by Christian Surieux
 * $Source: ?/ADNCRM.Core/Country/CountryInfoSerializer.cs $
 * $log$
 * Revision 6 2010/08/06 19:56:28  christian.surieux
 *   Added Log to All cs files
 */

using System;
using System.Diagnostics;
using System.IO;
using System.Xml;
using System.Xml.Serialization;
using System.Globalization;

using AddonNice.Diagnostics;

namespace AddonNice.Country
{
	/// <summary>
	/// CountryInfoSerializer manages serialization of
	/// Country list
	/// </summary>
	public class CountryInfoSerializer
	{
		public static XmlDocument Serialize(CountryInfo[] countries)
		{
			XmlSerializer serializer = new XmlSerializer(typeof(CountryInfo[]));
			
			MemoryStream writer = new MemoryStream();
			serializer.Serialize(writer, countries);

			XmlDocument x = new XmlDocument();
			writer.Position = 0;
			x.Load(writer);

			writer.Close();

			return x;
		}

		public static XmlDocument Serialize(CountryInfo country)
		{
			XmlSerializer serializer = new XmlSerializer(typeof(CountryInfo));
			
			MemoryStream writer = new MemoryStream();
			serializer.Serialize(writer, country);

			XmlDocument x = new XmlDocument();
			writer.Position = 0;
			x.Load(writer);

			writer.Close();

			return x;
		}

		public static CountryInfo[] Deserialize(System.IO.Stream countriesStream)
		{
			XmlSerializer serializer = new XmlSerializer(typeof(CountryInfo[]));
			CountryInfo[] countries = null;
			try
			{
				countries = (CountryInfo[]) serializer.Deserialize(countriesStream);
			}
			catch(Exception ex)
			{
				if ( CountryTraceSwitch.Sw.TraceError )
                    Trace.WriteLine( string.Format(CultureInfo.InvariantCulture,"Deserialize 0 ex :{0}",ex),CountryTraceSwitch.Sw.Info);
					throw;
			}

			//Serializer cannot set parent values so...
			RecurseSetParentValues(countries, null);

			return countries;
		}

		public static CountryInfo[] Deserialize(System.IO.TextReader countriesStream)
		{
			XmlSerializer serializer = new XmlSerializer(typeof(CountryInfo[]));
			CountryInfo[] countries = null;
			try
			{
				countries = (CountryInfo[]) serializer.Deserialize(countriesStream);
			}
			catch(Exception ex)
			{
				if ( CountryTraceSwitch.Sw.TraceError )
                    Trace.WriteLine( string.Format(CultureInfo.InvariantCulture,"Deserialize 1 ex :{0}",ex),CountryTraceSwitch.Sw.Info);
					throw;
			}

			//Serializer cannot set parent values so...
			RecurseSetParentValues(countries, null);

			return countries;
		}

		public static CountryInfo[] Deserialize(System.Xml.XmlReader countriesStream)
		{
			XmlSerializer serializer = new XmlSerializer(typeof(CountryInfo[]));
			CountryInfo[] countries = null;
			try
			{
				countries = (CountryInfo[]) serializer.Deserialize(countriesStream);
			}
			catch(Exception ex)
			{
				if ( CountryTraceSwitch.Sw.TraceError )
                    Trace.WriteLine( string.Format(CultureInfo.InvariantCulture,"Deserialize 2 ex :{0}",ex),CountryTraceSwitch.Sw.Info);
				throw;
			}

			//Serializer cannot set parent values so...
			RecurseSetParentValues(countries, null);

			return countries;
		}

		private static void RecurseSetParentValues(CountryInfo[] countries, CountryInfo currentParent)
		{
			foreach(CountryInfo c in countries)
			{
				c.SetParentCountryInfo(currentParent);

				if (c.Childs != null)
				{
					RecurseSetParentValues(c.Childs, c);
				}
			}
		}

	}
}
